﻿using System;
using System.Collections.Generic;
using System.Text;
using WeiboFilter.Models;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace WeiboFilter.DAL
{
    public class WordsDAL
    {
        private string sp_insertWordInfo = "dbo.usp_insertWordInfo";
        private string sp_getWordInfo = "dbo.usp_getWordInfo";
        private string sp_updateWordInfo = "dbo.usp_updateWordInfo";
        private string sp_getTopWordInfo = "dbo.getWordInfos";
        private string customerId;

        public WordsDAL(string customerId)
        {
            this.customerId = customerId;
            
        }

        //更新或添加
        public Word UpdateWordInfo(Word wo)
        {
            //从数据库中
            Word temp = GetWordInfo(wo.content);
            if (temp == null)
            {//数据库中没有
                return InsertWordInfo(wo);
            }
            //加权平均
            wo.point = temp.point * temp.times + wo.point*wo.times;
            wo.times = wo.times + temp.times;
            wo.point = wo.point / wo.times;

            SqlParameter[] Params = new SqlParameter[]{
                new SqlParameter("@customer_id",customerId),
                new SqlParameter("@word",wo.content),//确认一条记录
                new SqlParameter("@point",wo.point),
                new SqlParameter("@times",wo.times)

            };
            int ret = SqlHelper.ExecuteNonQuery(SqlCommon.connString, CommandType.StoredProcedure, sp_updateWordInfo, Params);

            return wo;

        }

        public Word InsertWordInfo(Word wo)
        {
            SqlParameter[] Params = new SqlParameter[]{
                new SqlParameter("@customer_id",customerId),
                new SqlParameter("@word",wo.content),//确认一条记录
                new SqlParameter("@point",wo.point),
                new SqlParameter("@times",wo.times)

            };
            int ret = SqlHelper.ExecuteNonQuery(SqlCommon.connString, CommandType.StoredProcedure, sp_insertWordInfo, Params);

            return wo;
        }

        public Word GetWordInfo(string w)
        {
            Word wo=null;
            SqlParameter[] Params = new SqlParameter[]{
                new SqlParameter("@customer_id",customerId),
                new SqlParameter("@word",w)
            };
            using(SqlDataReader sdr=SqlHelper.ExecuteReader(SqlCommon.connString,CommandType.StoredProcedure,sp_getWordInfo,Params))
            {//唯一性
                if (!sdr.HasRows)
                    return null;
                wo = new Word(); 
                sdr.Read();
                wo.content = sdr.GetString(0);
                wo.point = (double)sdr.GetDecimal(1);
                wo.times = sdr.GetInt32(2);
            }
            return wo;
        }

        public IList<Word> GetTopWordInfo(int n,int Like)
        {

            List<Word> l = new List<Word>();
            SqlParameter[] Params = new SqlParameter[]{
                    new SqlParameter("@count",n),
                    new SqlParameter("@Bo",Like)
            };
            using(SqlDataReader sdr=SqlHelper.ExecuteReader(SqlCommon.connString,CommandType.StoredProcedure,sp_getTopWordInfo,Params))
            {
                while (sdr.Read())
                {
                    Word w = new Word();
                    w.content = sdr.GetString(0);
                    w.point =(double) sdr.GetDecimal(1);
                    w.times = sdr.GetInt32(2);
                    l.Add(w);
                }
            }
            return l;

        }
    }
}
